from django.contrib import admin
from .models import *
import logging

# Register your models here.
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):

    actions = ['push_article']

    list_display=('id', 'article_name', 'link_url', 'logo', 'source', 'author', 'article_time', 'colored_status')
    list_filter = ('status',  'source')  # 过滤器
    #list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 20
    # 设置哪些字段可以点击进入编辑界面
    list_display_links = ('id', 'article_name')

    def push_article(self, request, queryset):
        queryset_list = []
        id_list = []
        for i in queryset.values():
            if i['status'] != 1:
                article = Article(
                    article_name = i['article_name'],
                    url = i['url'],
                    logo = i['logo'],
                    author = i['author'],
                    source = i['source'],
                    status = 1,
                    group_id = 1,
                    article_time = i['article_time']
                )
                queryset_list.append(article)
                id_list.append(i['id'])
        Article.objects.using('app_db').bulk_create(queryset_list)
        Article.objects.filter(id__in=id_list).update(status=1)
    push_article.short_description = "推送所选文章"